from sqlalchemy import Column, Integer, String, DateTime, Boolean
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime, timezone, timedelta

Base = declarative_base()

# 中国标准时间时区
CST = timezone(timedelta(hours=8))

def get_cst_now():
    """获取中国标准时间"""
    return datetime.now(CST)

class User(Base):
    """用户表"""
    __tablename__ = "t_user"
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment="主键自增ID")
    user_id = Column(String(64), nullable=False, unique=True, comment="用户ID (unionid)")
    energy = Column(Integer, nullable=False, default=0, comment="用户能量值")
    created_at = Column(DateTime, nullable=False, default=get_cst_now, comment="创建时间")
    updated_at = Column(DateTime, nullable=False, default=get_cst_now, onupdate=get_cst_now, comment="修改时间")
    is_deleted = Column(Boolean, nullable=False, default=False, comment="删除标记")
    
    def to_dict(self):
        return {
            "id": self.id,
            "user_id": self.user_id,
            "energy": self.energy,
            "created_at": self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            "updated_at": self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None,
            "is_deleted": self.is_deleted
        } 